package com.devunion.reader.client.command;

import com.devunion.reader.client.core.AsyncCommand;
import com.devunion.reader.client.to.ProjectTreeTo;
import com.devunion.reader.client.to.SearchProjectTo;
import com.devunion.reader.client.service.ProjectService;
import com.extjs.gxt.ui.client.data.DataProxy;
import com.extjs.gxt.ui.client.data.DataReader;
import com.google.gwt.user.client.rpc.AsyncCallback;

import java.util.List;

/**
 * @author Timoshenko Alexander 04.07.2009 : 19:18:47
 */
public class ProjectTreeInfoCmd extends AsyncCommand<List<ProjectTreeTo>>
        implements DataProxy<ProjectTreeTo, List<ProjectTreeTo>> {
    private ProjectTreeTo projectTreeTo;
    private AsyncCallback<List<ProjectTreeTo>> listAsyncCallback;
    private SearchProjectTo searchProjectTo;

    public void setSearchProjectTo(SearchProjectTo searchProjectTo) {
        this.searchProjectTo = searchProjectTo;
    }    

    public void attempt() {
        ProjectService.App.getInstance()
                .getProjectTreeItems(projectTreeTo, searchProjectTo, this);
    }

    public void oncapture(List<ProjectTreeTo> value) {
        this.listAsyncCallback.onSuccess(value);
    }

    public void load(DataReader<ProjectTreeTo, List<ProjectTreeTo>> projectTreeToListDataReader,
                     ProjectTreeTo projectTreeTo, AsyncCallback<List<ProjectTreeTo>> listAsyncCallback) {
        this.projectTreeTo = projectTreeTo;
        this.listAsyncCallback = listAsyncCallback;

        attempt();
    }
}
